package 复习.博客.回溯算法.组合问题;

import java.util.ArrayList;
import java.util.List;

public class 组合总和3 {
    int sum;
    List<Integer> list = new ArrayList<>();
    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        backStacking(k,n,1);
        return lists;
    }

    public void backStacking(int k,int n,int index){
        if(sum == n && list.size() == k){
            lists.add(new ArrayList<>(list));
        }
        for (int i = index; i < 9 - (k - list.size()) + 1 && sum + i <= n; i++) {
            list.add(i);
            backStacking(k,n,i+1);
            list.remove(list.size() - 1);
        }
    }
}
